from datetime import datetime

from django.db import models
from django.contrib.auth.models import AbstractUser



class BaseModel(models.Model):
    created_time = models.DateTimeField('创建时间',auto_now_add=True)
    update_time = models.DateTimeField('更新时间',auto_now=True)

# 部门
class Department(BaseModel):
    name = models.CharField(max_length=30)
    class Meta:
        db_table = 'department'
    def __str__(self):
        return self.name

# 职务
class Duty(BaseModel):
    name = models.CharField(max_length=30, verbose_name='职务名称')
    class Meta:
        db_table = 'duty'
    def __str__(self):
        return self.name

# 员工
class Staff(models.Model):  # 不能也继承BaseModel，会冲突
    SEX_CHOICE = (
        (1, '男'),
        (2, '女')
    )
    department = models.ForeignKey(Department, on_delete=models.CASCADE)
    duty = models.ForeignKey(Duty, on_delete=models.CASCADE)
    name = models.CharField(max_length=30, verbose_name='姓名')
    sex = models.IntegerField(choices=SEX_CHOICE, default=1, verbose_name='性别')
    phone = models.CharField(max_length=11, verbose_name='手机号')
    email = models.EmailField(max_length=60, verbose_name='邮箱')
    class Meta:
        db_table = 'staff'
    def __str__(self):
        return self.name




# 权限类型
class PermissionType(models.Model):
    name = models.CharField('权限类型',max_length=30)
    class Meta:
        db_table = 'permission_type'
        verbose_name_plural = '权限类别'
    def __str__(self):
        return self.name

# 用户模型类
class User(AbstractUser):
    mobile = models.CharField(max_length=11,verbose_name='手机号')
    last_login = models.DateTimeField('上次登录时间',default=datetime.now())
    # 角色
    role = models.ManyToManyField(PermissionType)
    class Meta:
        db_table = 'user'
    def __str__(self):
        return self.username

# 权限
class Permission(models.Model):
    title = models.CharField('权限名称',max_length=30)
    code_name = models.CharField('权限地址',max_length=300)
    type_name = models.ForeignKey(PermissionType,on_delete=models.CASCADE,verbose_name='权限类型')
    class Meta:
        db_table = 'permission'
        verbose_name_plural = '权限表'
    def __str__(self):
        return self.title

# 用户组
class UserGroup(models.Model):
    name = models.CharField('组名',max_length=30)
    user = models.ManyToManyField(User)  # 关系字段 多对多
    permission = models.ManyToManyField(Permission)
    class Meta:
        db_table = 'user_group'
        verbose_name_plural = '用户组'
    def __str__(self):
        return self.name

# 用户头像
class Head(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户头像')
    avatar = models.CharField('头像路径',max_length=300)
    class Meta:
        db_table = 'head'
        verbose_name_plural = '用户头像'
    def __str__(self):
        return self.user
